package com.beyondbit.demo.service.impl;

import com.beyondbit.demo.common.IdGenerator;
import com.beyondbit.demo.common.Util;
import com.beyondbit.demo.dao.DictionaryMapper;
import com.beyondbit.demo.entity.Dictionary;
import com.beyondbit.demo.entity.query.DictionaryQuery;
import com.beyondbit.demo.service.DictionaryService;
import com.beyondbit.web.infrastructure.entity.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author lfx
 * @version 1.0
 * @date 2021/12/6 13:34
 */
@Service("buaDictionaryService")
public class DictionaryServiceImpl implements DictionaryService {
    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource
    private IdGenerator idGenerator;

    /**
     * 通过ID查询单条数据
     *
     * @param dicId 主键
     * @return 实例对象
     */
    @Override
    public Dictionary queryById(String dicId) {
        return this.dictionaryMapper.queryById(dicId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Dictionary> queryAllByLimit(int offset, int limit) {
        return this.dictionaryMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param dictionary 实例对象
     * @return 实例对象
     */
    @Override
    public Dictionary insert(Dictionary dictionary) {
        dictionary.setDicId(idGenerator.newId());
        dictionary.setCreateTime(new Date());
        dictionary.setDicIsSys("0");
        dictionary.setDicSequence(Util.getPointValue(dictionary.getDicSequence(),2,"0"));
        dictionary.setCreateUserName("test");
        dictionary.setCreateTime(new Date());
        dictionary.setDicPath("testpath");
        this.dictionaryMapper.insert(dictionary);
        return dictionary;
    }

    /**
     * 修改数据
     *
     * @param dictionary 实例对象
     * @return 实例对象
     */
    @Override
    public Dictionary update(Dictionary dictionary) {
        this.dictionaryMapper.update(dictionary);
        return this.queryById(dictionary.getDicId());
    }

    /**
     * 通过主键删除数据
     *
     * @param dicId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String dicId) {
        return this.dictionaryMapper.deleteById(dicId) > 0;
    }

    @Override
    public Page<Dictionary> query(DictionaryQuery query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<Dictionary> list = dictionaryMapper.query(query);

        return Page.of(list);
    }

    @Override
    public Object getDicTree(List<Dictionary> menus) {
        List<Map<String,Object>> list = new LinkedList<>();
        menus.forEach(menu -> {
                    if (menu!=null){
                        List<Dictionary> menuList = dictionaryMapper.getDicTree(menu.getDicId());
                        Map<String,Object> map = new HashMap<>(16);
                        map.put("id",menu.getDicId());
                        map.put("label",menu.getDicName());
                        map.put("pid",menu.getDicParentId());
                        if(menuList!=null && menuList.size()!=0){
                            map.put("children",getDicTree(menuList));
                        }
                        list.add(map);
                    }
                }
        );
        return list;
    }

    @Override
    public List<Dictionary> getNodeTree(String treeId) {
        List<Dictionary> nodeTree = dictionaryMapper.getDicTree(treeId);
        return  nodeTree;
    }
}
